package com.study.config;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.pojo.BaseEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * mybatis plus代码自动生成配置
 * @author HeWei123
 */
public class MybatisPlus {

    private static final String file = "E:\\Project\\study\\messageCenter\\extern_service\\src\\main\\java";

    private static final String xmlUrl = "E:\\Project\\study\\messageCenter\\extern_service\\src\\main\\resources\\mapper";

    private static final String pojoUrl = "E:\\Project\\study\\messageCenter\\extern_pojo\\src\\main\\java\\com\\pojo\\entity";

    /**
     //     * <p>
     //     * 读取控制台内容
     //     * </p>
     //     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (StringUtils.isNotBlank(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {

        AutoGenerator mpg = new AutoGenerator();

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1/member?&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false");
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("root");
        mpg.setDataSource(dataSourceConfig);



        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(file);
        // 开启 BaseResultMap
        gc.setBaseResultMap(true);
        // 开启 baseColumnList
        gc.setBaseColumnList(true);
        //作者
        gc.setAuthor("admin");
        //公共父类
        gc.setOpen(false);
        //自定义实体命名方式，注意 %s 会自动填充表实体属性！
        gc.setEntityName("%sEntity");

        gc.setIdType(IdType.ASSIGN_ID);
        mpg.setGlobalConfig(gc);



        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("study");
        pc.setParent("com");
        mpg.setPackageInfo(pc);


        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {} };


        // 自定义输出配置
        List<FileOutConfig> focList = new ArrayList<>();
        // xml自定义配置会被优先输出
        focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名
                return  xmlUrl+ "/" + tableInfo.getXmlName()  + StringPool.DOT_XML;
        }
        });

        // pojo自定义配置会被优先输出
        focList.add(new FileOutConfig("/templates/entity.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输出文件名
                return  pojoUrl+ "/" + tableInfo.getEntityName()  + StringPool.DOT_JAVA;
            }
        });


        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        // 配置模板
        TemplateConfig templateConfig = new TemplateConfig();
        // 已配置自定义输出 不想重复输出 可置为null 则不输出
        templateConfig.setXml(null);
        templateConfig.setEntity(null);
        templateConfig.setController(null);
        mpg.setTemplate(templateConfig);

        StrategyConfig strategy = new StrategyConfig();
        strategy .setSuperEntityClass(BaseEntity.class);
        strategy.setInclude(scanner("表名，多个英文逗号分割").split(","));
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.setStrategy(strategy);
        mpg.execute();
    }
}
